package com.dc.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.annotation.DataScope;
import com.dc.common.constant.CustomSqlConstants;
import com.dc.common.utils.PageUtil;
import com.dc.common.vo.ResultVo;
import com.dc.project.system.dao.SysPostDao;
import com.dc.project.system.dto.SysPostDto;
import com.dc.project.system.dto.SysUserPostDto;
import com.dc.project.system.entity.SysPost;
import com.dc.project.system.entity.SysUserPost;
import com.dc.project.system.service.SysPostService;
import com.dc.project.system.service.SysUserPostService;
import com.dc.project.system.vo.SysUserPostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统用户岗位表(SysPost)表服务实现类
 *
 * @author zhuangcy
 * @since 2021-06-08 12:37:11
 */
@Service("sysPostService")
public class SysPostServiceImpl extends ServiceImpl<SysPostDao, SysPost> implements SysPostService {
    @Autowired
    private SysUserPostService sysUserPostService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @Override
    public ResultVo queryById(Serializable id) {
        return ResultVo.success().data(this.getById(id));
    }

    /**
     * 分页查询系统用户岗位表
     *
     * @param dto
     * @return
     */
    @DataScope
    @Override
    public ResultVo queryByPage(SysPostDto dto) {
        QueryWrapper<SysPost> queryWrapper = new QueryWrapper<>();
        IPage<SysPost> page = this.page(PageUtil.convert2PageParam(dto), queryWrapper);
        return ResultVo.success().data(PageUtil.convert2PageResult(page));
    }

    /**
     * 新增系统用户岗位表
     *
     * @param sysPost
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo add(SysPost sysPost) {
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPost::getPostCode, sysPost.getPostCode())
                .last(CustomSqlConstants.LIMIT_1);
        SysPost one = this.getOne(queryWrapper, false);
        if (null != one) {
            return ResultVo.failed().msg("该编码已存在");
        }
        return ResultVo.verify(this.save(sysPost));
    }

    /**
     * 编辑系统用户岗位表
     *
     * @param sysPost
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo edit(SysPost sysPost) {
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPost::getPostCode, sysPost.getPostCode())
                .last(CustomSqlConstants.LIMIT_1);
        SysPost one = this.getOne(queryWrapper, false);
        if (null != one && !one.getPostId().equals(sysPost.getPostId())) {
            return ResultVo.failed().msg("该编码已存在");
        }
        return ResultVo.verify(this.updateById(sysPost));
    }

    /**
     * 根据主键删除系统用户岗位表
     *
     * @param id 主键
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo delete(Integer id) {
        // 删除时校验是否已被用户使用
        if (sysUserPostService.existUser(id)) {
            return ResultVo.failed().msg("该岗位已被使用，无法删除");
        }
        return ResultVo.verify(this.removeById(id));
    }

    /**
     * 获取用户岗位
     *
     * @param dto
     * @return
     */
    @Override
    public ResultVo getUserPost(SysUserPostDto dto) {
        List<SysPost> posts = this.list();
        LambdaQueryWrapper<SysUserPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserPost::getUserId, dto.getUserId());
        List<Integer> postIds = sysUserPostService.list(queryWrapper).stream().map(SysUserPost::getPostId).collect(Collectors.toList());
        HashMap<String, Object> result = new HashMap<>(2);
        result.put("posts", posts);
        result.put("postIds", postIds);
        return ResultVo.success().data(result);
    }

    /**
     * 根据用户id获取用户岗位
     *
     * @param userId
     * @return
     */
    @Override
    public ResultVo getUserPostByUserId(Integer userId) {
        List<SysUserPostVo> list = baseMapper.getUserPostByUserId(userId);
        return ResultVo.success().data(list);
    }
}